﻿using System;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Threading;
using Microsoft.Practices.ServiceLocation;
using SAFPodcast.WP8.Model.Services;

namespace SAFPodcast.WP8.ViewModel
{
    public class AddFeedViewModel : ViewModelBase
    {
        private IDataService _dataService;

        public AddFeedViewModel(IDataService dataService)
        {
            addHidden = new RelayCommand(Add);
            cancelHidden = new RelayCommand(Cancel);
            testFeed = new RelayCommand(TestFeedMethod);

            //UrlAsString = "http://localhost/ruquier.xml";
            UrlAsString = "http://www.europe1.fr/podcasts/on-va-s-gener.xml";

            this._dataService = dataService;
        }

        #region AddHidden
        private RelayCommand addHidden;
        public RelayCommand AddHidden
        {
            get { return addHidden; }
        }

        public void Add()
        {
            Uri uri = null;
            if (Uri.TryCreate(UrlAsString, UriKind.Absolute, out uri))
            {
                _dataService.GetFeedName(uri,
                    (name, ex) =>
                    {
                        if (ex == null)
                        {
                            var newFeed = _dataService.AddFeed(UrlAsString, name);
                            var vm = ServiceLocator.Current.GetInstance<MainViewModel>();
                            vm.Feeds.Add(newFeed);
                        }
                        else
                        {
                            MessageBox.Show("An error occured while loading field data.", "Check your input", MessageBoxButton.OK);
                        }
                    });
                this.MessengerInstance.Send(false, "NavigationBackRequest");
            }
            else
            {
                MessageBox.Show("The URL is not valid.", "URI", MessageBoxButton.OK);
            }
        }
        #endregion
        #region CancelHidden
        private RelayCommand cancelHidden;
        public RelayCommand CancelHidden
        {
            get { return cancelHidden; }
        }

        public void Cancel()
        {
            this.MessengerInstance.Send(false, "NavigationBackRequest");
        }
        #endregion
        #region TestFeed
        private RelayCommand testFeed;
        public RelayCommand TestFeed
        {
            get { return testFeed; }
        }

        public void TestFeedMethod()
        {
            Uri uri = null;
            if (Uri.TryCreate(UrlAsString, UriKind.Absolute, out uri))
            {
                _dataService.TestFeed(uri, TestFeedCallback);
            }
            else
            {
                MessageBox.Show("The URL is not valid.", "URI", MessageBoxButton.OK);
            }
        }

        public void TestFeedCallback(bool result)
        {
            DispatcherHelper.RunAsync(() =>
            {
                if (result)
                {
                    TestResult = "Success !";
                }
                else
                {
                    TestResult = "Failed...";
                }
            });
        }
        #endregion
        #region UrlAsString
        private const String UrlAsStringPropertyName = "UrlAsString";
        private String urlAsString;
        public String UrlAsString
        {
            get { return urlAsString; }
            set
            {
                Set(UrlAsStringPropertyName, ref urlAsString, value);
            }
        }
        #endregion
        #region TestResult
        private const String TestResultPropertyName = "TestResult";
        private String testResult;
        public String TestResult
        {
            get { return testResult; }
            set
            {
                Set(TestResultPropertyName, ref testResult, value);
            }
        }
        #endregion
    }
}
